package com.sunda.spmsweb.ordercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.service.IOrderKeyValueConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 订单请购码值配置信息表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2020-12-22
 */
@RestController
@RequestMapping("/orderKVConfig")
@Api(tags="获取订单键值对配置",description = "获取订单键值对配置")
public class OrderKeyValueConfigController {

    @Autowired
    IOrderKeyValueConfigService iOrderKeyValueConfigService;

    @RequestMapping("/getAllConfig")
    @ApiOperation(value = "获取订单键值对配置", notes = "获取订单键值对配置", httpMethod = "GET")
    public ResponseResult getAllConfig(){
        try{
            return ResponseResult.success().add("kvList",iOrderKeyValueConfigService.getAllKVConfig());
        }catch(Exception e){
            return ResponseResult.error("获取数据失败");
        }
    }

    @RequestMapping("/getByFieldKey")
    @ApiOperation(value = "根据字段值获取键值对配置信息", notes = "根据字段值获取键值对配置信息\nfieldKey = projectCategory", httpMethod = "POST")
    public ResponseResult getByFieldKey(String fieldKey){
        try{
            return ResponseResult.success().add("kvList",iOrderKeyValueConfigService.getByFieldKey(fieldKey));
        }catch(Exception e){
            return ResponseResult.error("获取数据失败");
        }
    }

    @RequestMapping("/createKVConfig")
    @ApiOperation(value = "更新/新增键值对配置", notes = "更新/新增键值对配置\n" +
            "{\n" +
            "\t\"uuid\": \"06a6373233dd4f3b861af961f747524f\",\n" +
            "\t\"fieldKey\": \"orderWerksSPMSStatus\",\n" +
            "\t\"fieldName\": \"test工厂请购单SPMS状态\",\n" +
            "\t\"keyCode\": \"4\",\n" +
            "\t\"keyCodeDesc\": \"OA审批不通过\",\n" +
            "\t\"keyCodeEnDesc\": \"OA approval failed\",\n" +
            "\t\"enabled\": \"1\",\n" +
            "\t\"sortNo\": 1000,\n" +
            "\t\"remark\": null\n" +
            "}", httpMethod = "POST")
    public ResponseResult createKVConfig(@RequestBody JSONObject jsObject){
        try{
            return iOrderKeyValueConfigService.createKVConfig(jsObject);
        }catch(Exception e){
            return ResponseResult.error();
        }

    }

    @RequestMapping("/deleteKCConfig")
    @ApiOperation(value = "删除键值对配置", notes = "删除键值对配置", httpMethod = "POST")
    public ResponseResult deleteKCConfig(String uuid){
        try{
            return iOrderKeyValueConfigService.deleteKCConfig(uuid);
        }catch(Exception e){
            return ResponseResult.error();
        }

    }
}
